﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;

public partial class SLAMonitoring : ICPRelationshipPage
{
    
    public DataTable m_dtSLA;


    #region Properties
    
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, SLA Monitoring";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.SLA_CUSTOMERID] = null;
            Session[KSession.SLA_CARID] = null;
            Session[KSession.SLA_START] = null;
            Session[KSession.SLA_END] = null;
        }
        InitSLA();    
    }
    protected void dgSLA_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("FILTER"))
        {
            HandleFilter(e.Parameters);
        }
        else if (e.Parameters.ToUpper().StartsWith("ALL"))
        {
            HandleAll();
        }
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        String sxCustomerId =
            Session[KSession.SLA_CUSTOMERID] != null ?
            Session[KSession.SLA_CUSTOMERID].ToString() :
            String.Empty;
        String sxCarId =
            Session[KSession.SLA_CARID] != null ?
            Session[KSession.SLA_CARID].ToString() :
            String.Empty;
        DateTime dtmStart = Session[KSession.SLA_START] != null ?
            Convert.ToDateTime(Session[KSession.SLA_START]) :
            DateTime.Now;
        DateTime dtmEnd = Session[KSession.SLA_END] != null ?
            Convert.ToDateTime(Session[KSession.SLA_END]) :
            DateTime.Now;
        String sxStartDate = dtmStart.ToString("dd/MMM/yyyy");
        String sxEndDate = dtmEnd.ToString("dd/MMM/yyyy");
        String sxCompanyName = String.Empty;
        if (!String.IsNullOrEmpty(sxCustomerId))
        {
            sxCompanyName = "- " + DBQuery.CustomerTable.GetCompanyNameByID(Convert.ToInt32(sxCustomerId));
        }
        String sxCARName = String.Empty;
        if (!String.IsNullOrEmpty(sxCarId))
        {
            Cached.ICPCARTable cachedCAR = new Cached.ICPCARTable();
            sxCARName = cachedCAR.GetCARNameByID(Convert.ToInt32(sxCarId));
        }
        String sxFilename = String.Format(
            "SLA Report {0}{1} ({2} to {3})",
            sxCARName, sxCompanyName, sxStartDate, sxEndDate);
        
        dgExporter.WriteXlsToResponse(sxFilename);

        //btnExport.Text = BulkInsertManager.TryTheBulkInsert();

    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void InitSLA()
    {
        String sxCustomerId =
            Session[KSession.SLA_CUSTOMERID] != null ?
            Session[KSession.SLA_CUSTOMERID].ToString() : 
            String.Empty;
        String sxCarId =
            Session[KSession.SLA_CARID] != null ?
            Session[KSession.SLA_CARID].ToString() :
            String.Empty;
        DateTime dtmStart = Session[KSession.SLA_START] != null ?
            Convert.ToDateTime(Session[KSession.SLA_START]) :
            DateTime.Now;
        DateTime dtmEnd = Session[KSession.SLA_END] != null ?
            Convert.ToDateTime(Session[KSession.SLA_END]) :
            DateTime.Now;
        int iStartMonth = dtmStart.Month;
        int iStartYear = dtmStart.Year;
        int iEndMonth = dtmEnd.Month;
        int iEndYear = dtmEnd.Year;
        Cached.SLATable cachedSLA = new Cached.SLATable();
        m_dtSLA = cachedSLA.GetSLA(sxCustomerId, sxCarId, iStartMonth, iStartYear, iEndMonth, iEndYear);

        dgSLA.DataSource = m_dtSLA;
        dgSLA.DataBind();
    }
    protected void HandleFilter(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxCompanyId = String.Empty;
        String sxCARId = String.Empty;
        DateTime dtmStart = DateTime.Now;
        DateTime dtmEnd = DateTime.Now;
        if (!String.IsNullOrEmpty(sxSplitted[1]) && !String.IsNullOrEmpty(sxSplitted[2]))
        {
            sxCompanyId = sxSplitted[1];
            sxCARId = sxSplitted[2];
        }
        else if (!String.IsNullOrEmpty(sxSplitted[1]) && String.IsNullOrEmpty(sxSplitted[2]))
        {
            sxCompanyId = sxSplitted[1];
        }
        else if(!String.IsNullOrEmpty(sxSplitted[2]))
        {
            sxCARId = sxSplitted[2];
        }
        if (!String.IsNullOrEmpty(sxSplitted[3]))
        {
            dtmStart = Convert.ToDateTime(sxSplitted[3]);
        }
        if (!String.IsNullOrEmpty(sxSplitted[4]))
        {
            dtmEnd = Convert.ToDateTime(sxSplitted[4]);
        }
        Session[KSession.SLA_CUSTOMERID] = sxCompanyId;
        if (ICPUser.IsAdmin)
        {
            Session[KSession.SLA_CARID] = sxCARId;
        }
        else
        {
            if (String.IsNullOrEmpty(sxCARId))
            {
                Session[KSession.SLA_CARID] = ICPUser.ID;
            }
            else
            {
                Session[KSession.SLA_CARID] = sxCARId;
            }
        }
        Session[KSession.SLA_START] = dtmStart;
        Session[KSession.SLA_END] = dtmEnd;
        InitSLA();
    }
    protected void HandleAll()
    {
        Session[KSession.SLA_CUSTOMERID] = String.Empty;
        Session[KSession.SLA_CARID] = String.Empty;
        Session[KSession.SLA_START] = DateTime.Now;
        Session[KSession.SLA_END] = DateTime.Now;
        InitSLA();
    }
    public String GetMonthColumn(object objMonth)
    {
        String sxOutput = String.Empty;
        if (objMonth != DBNull.Value)
        {
            int iMonth = Convert.ToInt32(objMonth);
            DateTime dtmTmp = new DateTime(DateTime.Now.Year, iMonth, 1);
            sxOutput = dtmTmp.ToString("MMMM");
        }
        return sxOutput;
    }
   
    #endregion Custom Methods
}
